{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Array\n",
    "----"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Điểm ưu thế của NumPy so với Python thuần là có thể mô phỏng được mảng nhiều chiều\n",
    "\n",
    "Xem về [Mảng](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/list.ipynb) ở Python.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![NumPy Array](https://lh3.googleusercontent.com/FupEwjNSPGKcOgrw4yczZlcnOpnqZSyp0twsldHa22YOp8b9625ywBKIcxN5iq0peY84oEdsEqEU7AFGBHTE8Xz6AJLJLvaUr_gYz8XDtWOczcdymDCN2rABzspCIUWVP0d1N2lOsfbxXYE_Vyo6gGNTtqiHTjnBAVu3s8R5_jagCSEQerMst0TUPwrvoYmWgkuhHt-umtIS52CDLQa0d57wSa8Gtd8msgRBrjMYBWsOHhhk311LhE68TkusVmWscV-WpRV87kG0tjOQW02cWMC4sxcTctymq4fXsq2cDY1YPHeRuwFrViwTABroi_O9cJefeSzKIZJ2SSaodeRmhDnoshBpDlWSb9-KPSuTs4s-kZwc64V7XhKDV0aoRZFYC1FPYXDviQML-LXk8-g16WnI3ycznJHIwHFaa5XbG0g7c1Edy6N600TUqNMpJzAleq7EON1xcUHQ0rljTt5rky5NmU6DB1yvtH54gKUK8pVG4tlA12C6shkJnl93u1xwwJKeoKmn4MD7UqZNi3-DWzLaLKrhbz4gAM8kz_FthQIa7Y0R750OMS88lNy0tQwliRpNZS5-7-g3xxriVQ1DO8dAyzOY2ntFuE-KfmPH66etxbtm9PvZnsIcp-3QIST4CNu-P4Jmv7pSSqLlErNAxwIZT1nugJN26g=w798-h189-no)\n",
    "\n",
    "Ảnh từ [DataCamp](https://www.datacamp.com/community/tutorials/python-numpy-tutorial)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### I. Import thư viện NumPy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### II. Khởi tạo một Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 1. np.array()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### a. Mảng 1 chiều\n",
    "    - Bạn có thể liên hệ mảng 1 chiều với Vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_vector = np.array([1, 2, 3, 4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4,)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number_vector.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### b. Mảng 2 chiều\n",
    "    - Bạn có thể liên hệ mảng 2 chiều với ma trận 2 chiều"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_matrix = np.array([[1, 2, 3], [4, 5, 6]]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number_matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ma trận có số hàng là 2 và số cột là 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number_matrix.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### c. Mảng 3 chiều\n",
    "    - Bạn có thể liên hệ mảng 3 chiều với ma trận 3 chiều"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_3D_matrix = np.array([\n",
    "    [[1, 2, 3], [4, 5, 6]],\n",
    "    [[1, 2, 3], [4, 5, 6]]\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 2, 3)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number_3D_matrix.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 2. zeros() - ones() - full()\n",
    "    - Tạo ra mảng có các giá trị bằng nhau\n",
    "        \n",
    "        - zeros() tạo ra mảng các giá trị đều bằng 0\n",
    "            - Đầu vào là shape của array dưới dạng tuple\n",
    "            \n",
    "        - ones() tạo ra mảng các giá trị đều bằng 1\n",
    "            - Đầu vào là shape của array dưới dạng tuple\n",
    "        \n",
    "        - full() tạo ra mảng các giá trị bằng nhau và bằng một số cho trước\n",
    "            - Đầu vào là shape của array dưới dạng tuple và giá trị cho trước"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### a. Mảng 1 chiều"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.zeros()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_1D_zeros = np.zeros((7, ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number_1D_zeros"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.ones()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_1D_ones = np.ones((3, ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1.])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number_1D_ones"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.full()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_1D_sevens = np.full((6, ), 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([7, 7, 7, 7, 7, 7])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number_1D_sevens"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### b. Mảng 2 chiều"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.zeros()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_2D_zeros = np.zeros((7, 7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number_2D_zeros"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.ones()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_2D_ones = np.ones((6, 5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number_2D_ones"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.full()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_2D_eights = np.full((8, 8), 8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 8, 8, 8, 8, 8, 8, 8],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 8],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 8],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 8],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 8],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 8],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 8],\n",
       "       [8, 8, 8, 8, 8, 8, 8, 8]])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number_2D_eights"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### c. Mảng 3 chiều"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.zeros()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_3D_zeros = np.zeros((4, 2, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0., 0., 0.],\n",
       "        [0., 0., 0.]],\n",
       "\n",
       "       [[0., 0., 0.],\n",
       "        [0., 0., 0.]],\n",
       "\n",
       "       [[0., 0., 0.],\n",
       "        [0., 0., 0.]],\n",
       "\n",
       "       [[0., 0., 0.],\n",
       "        [0., 0., 0.]]])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number_3D_zeros"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.ones()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_3D_ones = np.ones((3, 3, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.]],\n",
       "\n",
       "       [[1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.]],\n",
       "\n",
       "       [[1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.]]])"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number_3D_ones"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "np.full()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_3D_twenties = np.full((3, 2, 7), 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[20, 20, 20, 20, 20, 20, 20],\n",
       "        [20, 20, 20, 20, 20, 20, 20]],\n",
       "\n",
       "       [[20, 20, 20, 20, 20, 20, 20],\n",
       "        [20, 20, 20, 20, 20, 20, 20]],\n",
       "\n",
       "       [[20, 20, 20, 20, 20, 20, 20],\n",
       "        [20, 20, 20, 20, 20, 20, 20]]])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number_3D_twenties"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 3. np.eye()\n",
    "    - Tạo ra một ma trận đơn vị"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.eye((10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 4. np.arange()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tạo 1 Array giá trị chạy từ 0 -> 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_1D_range_10 = np.arange(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number_1D_range_10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tạo 1 Array giá trị chạy từ 2 -> 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_1D_range_20 = np.arange(2, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number_1D_range_20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tạo 1 Array giá trị chạy từ 0 -> 30. Khoảng cách giữa các giá trị là 2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [],
   "source": [
    "number_1D_range_30 = np.arange(0, 30, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28])"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "number_1D_range_30"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 5. np.random.random()\n",
    "    - Tạo ra ma trận với các giá trị ngẫu nhiên"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.2416562 , 0.07738853, 0.48258709, 0.2607063 , 0.80988611],\n",
       "       [0.55631239, 0.51562934, 0.95095416, 0.59066717, 0.64751756],\n",
       "       [0.42497374, 0.11966676, 0.82407811, 0.80352517, 0.12991066],\n",
       "       [0.24715683, 0.29748386, 0.26677741, 0.02107929, 0.21032197],\n",
       "       [0.18406292, 0.86952751, 0.63418968, 0.07755376, 0.50483528],\n",
       "       [0.87096656, 0.04928014, 0.65223194, 0.63928117, 0.4443445 ],\n",
       "       [0.19519642, 0.55467875, 0.57888658, 0.01509584, 0.50863294],\n",
       "       [0.09283353, 0.54861298, 0.76249622, 0.70733076, 0.1887354 ],\n",
       "       [0.82271984, 0.53933904, 0.20685336, 0.42698788, 0.37528835],\n",
       "       [0.6270273 , 0.66702909, 0.69230398, 0.22863118, 0.63605925]])"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.random((10, 5))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
